--
-- Author:      name
-- DateTime:    2022-08-21 15:57:48
-- Description: 游戏主逻辑

local FSM = require "FSM"

local table_insert = table.insert
local table_remove = table.remove
local floor = math.floor
local random = math.random


local DeskBase = require("games.common.desk")
local Desk = DeskBase:extend("Desk")

function Desk:ctor()

	
end

----------------------------------------------------------------------------------------------
---------开始游戏
---------------------------------------------------------------------------------------------- 
function Desk:start()
	print("___开始游戏___")
	self.users = {}







	self.timer = new("timer")
	--配置信息
	--状态时间
	self.stateTime = {
		start = 2,		--开始
		play = 0,		--开始玩家操作
		over = 1, 		--结束游戏
		rest = 3,		--等待
	}
	--游戏状态
	--休息，开始玩家操作，抢庄, 下注，发牌，结算
	--state1 --eventName --state2 --callback
	local stateTable = {
		{"rest", 	"to_start", 	"start", 	function() self:stStart() end }, --state start
		{"start", 	"to_play",		"play", 	function() self:stPlay() end },
		{"play", 	"to_over",		"over", 	function() self:stOver() end },
		{"over", 	"to_rest",		"rest", 	function() self:stReset() end },
	}
	self.fsm = FSM.new(stateTable)

	--启动状态机
	self.fsm:set("over")
	self.fsm:fire("to_rest")
end

-------------------------------------------------------------------------------
-- private
-------------------------------------------------------------------------------
--重置数据
function Desk:reset()
	g.rule:shuffle()
	--设置在线玩家状态
end



-------------------------------------------------------------------------------
-- 推送消息
-------------------------------------------------------------------------------

function Desk:ntStart()
	--local user_list = g.user_manager:getUserList()
	--for user_id, v in pairs(user_list) do
	--	local data = { nnStartNt={
	-- 		desk_id = self.desk_id,
	-- 		time = self.stateTime.start,
	-- 		cards = self.tbPlayerCard[user_id],
	-- 		-- playingid_list = self:getPlayingid(),
	--	}}
	--	self:sendToUser(user_id, data)
	--end
	g.log:debug("__________ps_ntStart", data)
end

function Desk:ntRest()
	local data = {nnRestNt={
		desk_id = self.desk_id,
		time = self.stateTime.rest
	}}
	g.log:debug("_________ps_ntRest", data)
	--self:sendToAllUser(data)
end


-------------------------------------------------------------------------------
-- 状态
-------------------------------------------------------------------------------

--开始
function Desk:stStart()
	g.log:debug("_______ps onStart")		

	--设置在线玩家开始
	self.users = g.user_manager:getUserList() --正在玩的用户
	self.seat_user = g.user_manager:getSeatUser()

	local callback = function ()
		self.fsm:fire("to_play")
	end
	g.timeout(self.stateTime.start, callback)

end

function Desk:stPlay()
	g.log:debug("_______ps stPlay")
	self.isPlay = true
	local players = {}
	--红方开始走
	for k, v in pairs(self.users) do
		v:createAI()
		table_insert(players, v)
	end

	local player1 = players[1]
	player1:setCamp(1)
	local player2 = players[2]
	player2:setCamp(-1)
	local rule = g.rule
	rule:printMap()
	g.fork(function()
		for i = 1, 100 do
			print("####",i)
			if self.isPlay == false then
				break
			end
			player1:playByAI(player2)
			rule:printMap()
			if not self.isPlay then
				break
			end
			player2:playByAI(player1)
			rule:printMap()
			g.sleep(100)
		end
	end)

	--开个定时器，如果超时就


	g.log:debug("_____2__ps stPlay")


	--self.operStartTime = os.time()
	----通知客户端
	--self:ntWait()
	--
	--local function callback()
	--	local user_list = g.user_manager:getUserList()
	--	--人数不足时不开始 返回等待状态
	--	print("__111__onWait__",table.size(user_list) , self.min_player_num)
	--	if table.size(user_list) < self.min_player_num then
	--		return
	--	end
	--	self:stopTimer()
	--	self.fsm:fire("to_start")
	--end
	----等待一定时间后开始游戏
	--self:setTimer(self.stateTime.wait, callback, true)

end

--休息
function Desk:stReset()
	g.log:debug("_______ps onRest")
	--重置数据
	self:reset()
	self:ntRest()
	self:removeDisconnect()
	self:checkUser()

	local callback = function ( ... )
		local user_list = g.user_manager:getUserList()
		if table.size(user_list) < 2 then
			return
		end
		self.timer:stop()
		self.fsm:fire("to_start")
		print("222222222")
	end
	self.timer:start({
		sec = self.stateTime.rest,
		callback = callback,
		loop = true,
	})

end

function Desk:stOver()

end
----------------------------------
-- 数据操作
----------------------------------

--把不符合条件的玩家踢走
function Desk:checkUser()
	local user_list = g.user_manager:getUserList()
	for k,v in pairs(user_list) do
		print("----------checkUser ",v.uid, v.chip)
		if  v.chip < 1 then
			--local data = {nt_kick_table = {
			--	user_id = k,
			--	content = '金币不足请离场！',
			--	kick_reason = 1
			--}}
			--self:sendMessage(k,data)
		end

		if v.doNothingNum and v.doNothingNum >= self.kickDoNothingNum then
			--local data = {nt_kick_table = {
			--	user_id = k,
			--	content = '您已长时间未操作，将自动退出游戏。',
			--	kick_reason = 2,
			--}}
			--self:sendMessage(k,data)
		end
		----设置玩家所在桌子id为0
		--self:sendGate(v, 'update_desk_id', 0)
		--self:sitUp(k)
		--self.player_list[k] = nil
		--self:updatePlayerNum()
		--print("拼十踢走掉线 or 没准备好 or 筹码不够 or 太久没有操作#######",k)

	end
end

function Desk:showWin(my)
	self.isPlay = false
	if (my==1) then
		print("恭喜你，红方你赢了！");
	else
		print("很遗憾，黑方你输了！");
	end
end


return Desk